﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

public static class StringExt
{
    public static bool IsNullOrEmpty(this string str)
    {
        return string.IsNullOrEmpty(str);
    }
    public static T ToNum<T>(this string str, T defaultValue)
    {
        if (string.IsNullOrEmpty(str))
            return defaultValue;
        try
        {
            var result = (T)Convert.ChangeType(str, typeof(T));
            return result;
        }
        catch
        {
            return defaultValue;
        }
    }
    public static T? ToNum<T>(this string str) where T : struct
    {
        if (string.IsNullOrEmpty(str))
            return null;
        try
        {
            var result = (T)Convert.ChangeType(str, typeof(T));
            return result;
        }
        catch
        {
            return null;
        }
    }

    public static DateTime? ToDate(this string str)
    {
        DateTime d = DateTime.MinValue;
        if (DateTime.TryParse(str, out d))
        {
            return d;
        }
        return null;
    }
    public static DateTime ToDate(this string str, DateTime defaultValue)
    {
        return ToDate(str) ?? defaultValue;
    }

    /// <summary>
    /// 将一个对象转化为JSON
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string ToJson(this object obj)
    {
        return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
    }
    
    public static string ToJson(this object obj,Formatting formatting)
    {
        return Newtonsoft.Json.JsonConvert.SerializeObject(obj, formatting);
    }
    public static string ToJson(this object obj,Formatting formatting,params JsonConverter[] converters)
    {
        return Newtonsoft.Json.JsonConvert.SerializeObject(obj, formatting,converters);
    }
    public static string ToJson(this object obj,Formatting formatting,JsonSerializerSettings settings)
    {
        return Newtonsoft.Json.JsonConvert.SerializeObject(obj, formatting,settings);
    }

    public static bool Test(this string str, string pattern)
    {
        return Regex.IsMatch(str, pattern);
    }

    public static Match Match(this string str, string pattern)
    {
        return Regex.Match(str,pattern);
    }

    public static MatchCollection Matchs(this string str, string pattern)
    {
        return Regex.Matches(str, pattern);
    }

    public static string RegexReplace(this string str, string pattern, string to)
    {
        return Regex.Replace(str, pattern, to);
    }


    /// <summary>
    /// 显示文本不能超过指定的长度
    /// </summary>
    /// <param name="str"></param>
    /// <param name="length"></param>
    /// <returns></returns>
    public static string RemoveWithDot(this string str, int length)
    {
        return str.Length > length ? str.Remove(length) : str;
    }

    /// <summary>
    /// 去除字符串中的html
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string RemoveHtml(this string str)
    {
        //删除script
        str = Regex.Replace(str, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
        //删除HTML  
        str = Regex.Replace(str, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
        //删除空白
        str = Regex.Replace(str, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
        return str;
    }
    public static string ToChinese(this int num)
    {
        return NumToChinese.Get(num);
    }

    /// <summary>
    /// 将序号转化为字母，0=>A,1=>B
    /// </summary>
    /// <param name="num">要转换的数字</param>
    /// <returns></returns>
    public static string ToAbc(this int num)
    {
        return ToAbc(num, false);
    }
    /// <summary>
    /// 将序号转化为字母
    /// </summary>
    /// <param name="num">要转换的数字</param>
    /// <param name="oneBase">是否从0开始</param>
    /// <returns>oneBase:0=>A,1=>B
    /// !oneBase:1=>A,2=>B
    /// </returns>
    public static string ToAbc(this int num, bool oneBase)
    {
        return NumToAbc.Get(num, oneBase);
    }

    public static string ToBase64(this string str)
    {
        if (str.IsNullOrEmpty())
            return string.Empty;
        byte[] bytes = Encoding.Default.GetBytes(str);
        return Convert.ToBase64String(bytes);
    }
    public static string FromBase64(this string base64str)
    {
        if (base64str.IsNullOrEmpty())
            return string.Empty;

        try
        {
            byte[] outputb = Convert.FromBase64String(base64str);
            string orgStr = Encoding.Default.GetString(outputb);
            return orgStr;
        }
        catch
        {
            return string.Empty;
        }
    }
}

internal class NumToChinese
{
    private static string Switch(int i)
    {
        string ReturnCh = string.Empty;
        switch (i % 8)
        {
            case 1:
                ReturnCh = "十";
                break;
            case 2:
                ReturnCh = "百";
                break;
            case 3:
                ReturnCh = "千";
                break;
            case 4:
                ReturnCh = "万";
                break;
            case 5:
                ReturnCh = "十";
                break;
            case 6:
                ReturnCh = "百";
                break;
            case 7:
                ReturnCh = "千";
                break;
            case 0:
                ReturnCh = "亿";
                break;
            default:
                break;
        }
        return ReturnCh;
    }

    private static string Switch(char ch)
    {
        string ReturnCh = string.Empty;
        switch (ch)
        {
            case '0':
                ReturnCh = "零";
                break;
            case '1':
                ReturnCh = "一";
                break;
            case '2':
                ReturnCh = "二";
                break;
            case '3':
                ReturnCh = "三";
                break;
            case '4':
                ReturnCh = "四";
                break;
            case '5':
                ReturnCh = "五";
                break;
            case '6':
                ReturnCh = "六";
                break;
            case '7':
                ReturnCh = "七";
                break;
            case '8':
                ReturnCh = "八";
                break;
            case '9':
                ReturnCh = "九";
                break;
            default:
                break;
        }
        return ReturnCh;
    }
    public static string Get(double num)
    {
        string strNum = num.ToString();
        char[] Num = strNum.ToCharArray();
        Array.Reverse(Num);
        int length = Num.Length;
        string[] Result = new string[Num.Length];
        for (int i = 0; i < length; i++)
        {
            if (i == 0)
            {
                if (Switch(Num[i]) == "零")
                {
                    continue;
                }
                else
                {
                    Result[i] = Switch(Num[i]);
                }
            }
            else
            {
                if (Switch(Num[i]) == "零")
                {
                    Result[i] = "零";
                }
                else
                {
                    Result[i] = Switch(Num[i]) + Switch(i);
                }
            }

        }
        Array.Reverse(Result);
        string allResult = string.Empty;
        foreach (string a in Result)
        {
            allResult += a;
        }
        return Clearz(allResult);

    }

    //替换掉结果中多余的零
    private static string Clearz(string str)
    {
        System.Text.RegularExpressions.Regex regex = new Regex(@"零{1,}", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

        string strs = regex.Replace(str, "零");

        if (strs.Length > 0)
        {
            //检查最字符串的结尾
            System.Text.RegularExpressions.Regex Regtex = new Regex(@"$", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            return Regtex.Replace(strs, "");
        }
        return strs;

    }


}

internal class NumToAbc
{
    public static string Get(int num, bool oneBase)
    {
        if (oneBase)
            num = num - 1;
        if (num < 0 || num > 25)
            num = 0;
        return Convert.ToChar(num + 65).ToString();
    }
}



